<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>9 与其他软件的交互 | R语言笔记</title>
  <meta name="description" content="9 与其他软件的交互 | R语言笔记" />
  <meta name="generator" content="bookdown 0.19 and GitBook 2.6.7" />

  <meta property="og:title" content="9 与其他软件的交互 | R语言笔记" />
  <meta property="og:type" content="book" />
  
  
  
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="9 与其他软件的交互 | R语言笔记" />
  
  
  

<meta name="author" content="云腾足下" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="ML.html"/>
<link rel="next" href="DrawTips.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />









<script src="libs/htmlwidgets-1.5.1/htmlwidgets.js"></script>
<script src="libs/plotly-binding-4.9.2.1/plotly.js"></script>
<script src="libs/typedarray-0.1/typedarray.min.js"></script>
<link href="libs/crosstalk-1.1.0.1/css/crosstalk.css" rel="stylesheet" />
<script src="libs/crosstalk-1.1.0.1/js/crosstalk.min.js"></script>
<link href="libs/plotly-htmlwidgets-css-1.52.2/plotly-htmlwidgets.css" rel="stylesheet" />
<script src="libs/plotly-main-1.52.2/plotly-latest.min.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  jax: ["input/TeX","output/SVG"],
  extensions: ["tex2jax.js","MathMenu.js","MathZoom.js"],
  TeX: {
    extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
  }
});
</script>
<script type="text/javascript"
   src="../../../MathJax/MathJax.js">
</script>


<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>序</a></li>
<li class="chapter" data-level="1" data-path="PanelData.html"><a href="PanelData.html"><i class="fa fa-check"></i><b>1</b> 面板数据</a><ul>
<li class="chapter" data-level="1.1" data-path="PanelData.html"><a href="PanelData.html#一句话tips"><i class="fa fa-check"></i><b>1.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="1.2" data-path="PanelData.html"><a href="PanelData.html#plm包"><i class="fa fa-check"></i><b>1.2</b> <code>plm</code>包</a></li>
<li class="chapter" data-level="1.3" data-path="PanelData.html"><a href="PanelData.html#动态面板阈值估计r语言中有一个包dtp其估计函数为"><i class="fa fa-check"></i><b>1.3</b> 动态面板阈值估计：R语言中有一个包<code>dtp</code>，其估计函数为：</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="TS.html"><a href="TS.html"><i class="fa fa-check"></i><b>2</b> 普通回归和时间序列序列</a><ul>
<li class="chapter" data-level="2.1" data-path="TS.html"><a href="TS.html#一句话tips-1"><i class="fa fa-check"></i><b>2.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="2.2" data-path="TS.html"><a href="TS.html#gmm估计"><i class="fa fa-check"></i><b>2.2</b> GMM估计</a></li>
<li class="chapter" data-level="2.3" data-path="TS.html"><a href="TS.html#季节调整"><i class="fa fa-check"></i><b>2.3</b> 季节调整</a><ul>
<li class="chapter" data-level="2.3.1" data-path="TS.html"><a href="TS.html#r中有x12包可以做季节处理"><i class="fa fa-check"></i><b>2.3.1</b> R中有<code>x12</code>包可以做季节处理</a></li>
<li class="chapter" data-level="2.3.2" data-path="TS.html"><a href="TS.html#seasonal包有x13处理更加便捷"><i class="fa fa-check"></i><b>2.3.2</b> <code>seasonal</code>包有x13处理，更加便捷</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="DataProcess.html"><a href="DataProcess.html"><i class="fa fa-check"></i><b>3</b> 数据处理</a><ul>
<li class="chapter" data-level="3.1" data-path="DataProcess.html"><a href="DataProcess.html#一句话tips-2"><i class="fa fa-check"></i><b>3.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="3.2" data-path="DataProcess.html"><a href="DataProcess.html#rjsdmx包下载世界各大数据库数据"><i class="fa fa-check"></i><b>3.2</b> <code>RJSDMX</code>包下载世界各大数据库数据</a></li>
<li class="chapter" data-level="3.3" data-path="DataProcess.html"><a href="DataProcess.html#前向后向线性和样条插值"><i class="fa fa-check"></i><b>3.3</b> 前向、后向、线性和样条插值</a><ul>
<li class="chapter" data-level="3.3.1" data-path="DataProcess.html"><a href="DataProcess.html#signal包"><i class="fa fa-check"></i><b>3.3.1</b> <code>signal</code>包</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="统计.html"><a href="统计.html"><i class="fa fa-check"></i><b>4</b> 统计</a><ul>
<li class="chapter" data-level="4.1" data-path="统计.html"><a href="统计.html#一句话tips-3"><i class="fa fa-check"></i><b>4.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="4.2" data-path="统计.html"><a href="统计.html#mcmc算法"><i class="fa fa-check"></i><b>4.2</b> MCMC算法</a><ul>
<li class="chapter" data-level="4.2.1" data-path="统计.html"><a href="统计.html#吉布斯抽样原理"><i class="fa fa-check"></i><b>4.2.1</b> 吉布斯抽样原理</a></li>
<li class="chapter" data-level="4.2.2" data-path="统计.html"><a href="统计.html#一些共轭先验分布的结论"><i class="fa fa-check"></i><b>4.2.2</b> 一些共轭先验分布的结论</a></li>
<li class="chapter" data-level="4.2.3" data-path="统计.html"><a href="统计.html#一个吉布斯抽样的典型案例"><i class="fa fa-check"></i><b>4.2.3</b> 一个吉布斯抽样的典型案例</a></li>
<li class="chapter" data-level="4.2.4" data-path="统计.html"><a href="统计.html#metropolis-和-m-h算法"><i class="fa fa-check"></i><b>4.2.4</b> Metropolis 和 M-H算法</a></li>
<li class="chapter" data-level="4.2.5" data-path="统计.html"><a href="统计.html#一些带贝叶斯估计的r包使用报告"><i class="fa fa-check"></i><b>4.2.5</b> 一些带贝叶斯估计的R包使用报告</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="rawR.html"><a href="rawR.html"><i class="fa fa-check"></i><b>5</b> 原生的R</a><ul>
<li class="chapter" data-level="5.1" data-path="rawR.html"><a href="rawR.html#一句话tips-4"><i class="fa fa-check"></i><b>5.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="5.2" data-path="rawR.html"><a href="rawR.html#平行计算"><i class="fa fa-check"></i><b>5.2</b> 平行计算</a></li>
<li class="chapter" data-level="5.3" data-path="rawR.html"><a href="rawR.html#类和方法"><i class="fa fa-check"></i><b>5.3</b> 类和方法</a><ul>
<li class="chapter" data-level="5.3.1" data-path="rawR.html"><a href="rawR.html#s3类"><i class="fa fa-check"></i><b>5.3.1</b> S3类</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="rawR.html"><a href="rawR.html#打印到文件"><i class="fa fa-check"></i><b>5.4</b> 打印到文件</a></li>
<li class="chapter" data-level="5.5" data-path="rawR.html"><a href="rawR.html#apply函数族"><i class="fa fa-check"></i><b>5.5</b> <code>apply</code>函数族</a></li>
<li class="chapter" data-level="5.6" data-path="rawR.html"><a href="rawR.html#linux中的r"><i class="fa fa-check"></i><b>5.6</b> Linux中的R</a><ul>
<li class="chapter" data-level="5.6.1" data-path="rawR.html"><a href="rawR.html#如何修改.rprofile"><i class="fa fa-check"></i><b>5.6.1</b> 如何修改<code>.Rprofile</code>？</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="Eco.html"><a href="Eco.html"><i class="fa fa-check"></i><b>6</b> 经济学中的各种专业计算</a><ul>
<li class="chapter" data-level="6.1" data-path="Eco.html"><a href="Eco.html#一句话tips-5"><i class="fa fa-check"></i><b>6.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="6.2" data-path="Eco.html"><a href="Eco.html#wwz的贸易增加值分解"><i class="fa fa-check"></i><b>6.2</b> wwz的贸易增加值分解</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="math.html"><a href="math.html"><i class="fa fa-check"></i><b>7</b> 数学计算</a><ul>
<li class="chapter" data-level="7.1" data-path="math.html"><a href="math.html#一句话tips-6"><i class="fa fa-check"></i><b>7.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="7.2" data-path="math.html"><a href="math.html#数值优化"><i class="fa fa-check"></i><b>7.2</b> 数值优化</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="ML.html"><a href="ML.html"><i class="fa fa-check"></i><b>8</b> 机器学习和微观计量</a><ul>
<li class="chapter" data-level="8.1" data-path="ML.html"><a href="ML.html#一句话tips-7"><i class="fa fa-check"></i><b>8.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="8.2" data-path="ML.html"><a href="ML.html#roc曲线绘制"><i class="fa fa-check"></i><b>8.2</b> ROC曲线绘制</a></li>
<li class="chapter" data-level="8.3" data-path="ML.html"><a href="ML.html#randomforestsrc包使用报告"><i class="fa fa-check"></i><b>8.3</b> <code>randomForestSRC</code>包使用报告</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="otherSF.html"><a href="otherSF.html"><i class="fa fa-check"></i><b>9</b> 与其他软件的交互</a><ul>
<li class="chapter" data-level="9.1" data-path="otherSF.html"><a href="otherSF.html#一句话tips-8"><i class="fa fa-check"></i><b>9.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="9.2" data-path="otherSF.html"><a href="otherSF.html#r语言调用matlab"><i class="fa fa-check"></i><b>9.2</b> R语言调用Matlab</a><ul>
<li class="chapter" data-level="9.2.1" data-path="otherSF.html"><a href="otherSF.html#强大的r.matlab包"><i class="fa fa-check"></i><b>9.2.1</b> 强大的<code>R.matlab</code>包</a></li>
<li class="chapter" data-level="9.2.2" data-path="otherSF.html"><a href="otherSF.html#其他的一些小办法"><i class="fa fa-check"></i><b>9.2.2</b> 其他的一些小办法</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="otherSF.html"><a href="otherSF.html#r与python的无缝对接"><i class="fa fa-check"></i><b>9.3</b> R与Python的无缝对接</a><ul>
<li class="chapter" data-level="9.3.1" data-path="otherSF.html"><a href="otherSF.html#其他"><i class="fa fa-check"></i><b>9.3.1</b> 其他：</a></li>
</ul></li>
<li class="chapter" data-level="9.4" data-path="otherSF.html"><a href="otherSF.html#与julia的对接"><i class="fa fa-check"></i><b>9.4</b> 与julia的对接</a><ul>
<li class="chapter" data-level="9.4.1" data-path="otherSF.html"><a href="otherSF.html#juliacall包"><i class="fa fa-check"></i><b>9.4.1</b> <code>JuliaCall</code>包</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="DrawTips.html"><a href="DrawTips.html"><i class="fa fa-check"></i><b>10</b> 绘图技巧</a><ul>
<li class="chapter" data-level="10.1" data-path="DrawTips.html"><a href="DrawTips.html#d绘图"><i class="fa fa-check"></i><b>10.1</b> 3D绘图</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html"><i class="fa fa-check"></i><b>11</b> 创建包的一些建议</a><ul>
<li class="chapter" data-level="11.1" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html#一句话tips-9"><i class="fa fa-check"></i><b>11.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="11.2" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html#使用自己的数据集"><i class="fa fa-check"></i><b>11.2</b> 使用自己的数据集</a></li>
<li class="chapter" data-level="11.3" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html#测试"><i class="fa fa-check"></i><b>11.3</b> 测试</a></li>
<li class="chapter" data-level="11.4" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html#泛型函数"><i class="fa fa-check"></i><b>11.4</b> 泛型函数</a></li>
<li class="chapter" data-level="11.5" data-path="创建包的一些建议.html"><a href="创建包的一些建议.html#其他-1"><i class="fa fa-check"></i><b>11.5</b> 其他</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="其他-2.html"><a href="其他-2.html"><i class="fa fa-check"></i><b>12</b> 其他</a></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">R语言笔记</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="otherSF" class="section level1">
<h1><span class="header-section-number">9</span> 与其他软件的交互</h1>
<div id="一句话tips-8" class="section level2">
<h2><span class="header-section-number">9.1</span> 一句话Tips</h2>
<ul>
<li><code>stargazer</code>的一个模版调用：</li>
</ul>
<div class="sourceCode" id="cb51"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb51-1"><a href="otherSF.html#cb51-1"></a><span class="kw">stargazer</span>(regression,<span class="dt">type =</span> <span class="st">&#39;text&#39;</span>,<span class="dt">out =</span> <span class="st">&quot;../PicTab/cmp.html&quot;</span>,<span class="dt">no.space =</span> T,<span class="dt">report =</span> <span class="kw">c</span>(<span class="st">&#39;vcp&#39;</span>))</span></code></pre></div>
<p><code>report</code>意味着报告变量、系数与p值。</p>
<ul>
<li><code>officer</code>: 与微软软件互动的一个包</li>
<li><code>readstata13</code>包读Stata13以后的数据格式。</li>
<li>R语言调用stata</li>
</ul>
<p>用RStata包可以从R里面调用stata，不过要先用<code>chooseBinStata()</code>先设置stata的安装路径。 也可以在R的启动环境中进行配置。
注意在启动环境中（即Rprofile.site文件中）配置时，应增加如下一行，
<code>options(RStata.StataPath = "\"D:\\Program Files (x86)\\Stata14\\StataMP-64\"")</code></p>
<ul>
<li>R语言读取SPSS（中文字符）</li>
</ul>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb52-1"><a href="otherSF.html#cb52-1"></a><span class="co"># 读英文字符</span></span>
<span id="cb52-2"><a href="otherSF.html#cb52-2"></a><span class="kw">library</span>(foreign)  </span>
<span id="cb52-3"><a href="otherSF.html#cb52-3"></a>mydata=<span class="kw">read.spss</span>(<span class="st">&quot;data.sav&quot;</span>)  </span>
<span id="cb52-4"><a href="otherSF.html#cb52-4"></a><span class="co"># 或者如下</span></span>
<span id="cb52-5"><a href="otherSF.html#cb52-5"></a><span class="kw">library</span>(Hmisc)  </span>
<span id="cb52-6"><a href="otherSF.html#cb52-6"></a>data=<span class="kw">spss.get</span>(<span class="st">&quot;data.sav&quot;</span>) </span>
<span id="cb52-7"><a href="otherSF.html#cb52-7"></a></span>
<span id="cb52-8"><a href="otherSF.html#cb52-8"></a><span class="co"># 读中文字符</span></span>
<span id="cb52-9"><a href="otherSF.html#cb52-9"></a><span class="kw">library</span>(memisc)</span>
<span id="cb52-10"><a href="otherSF.html#cb52-10"></a>data1 =<span class="st"> </span><span class="kw">as.data.set</span>(<span class="kw">spss.system.file</span>(<span class="st">&quot;data.sav&quot;</span>))</span>
<span id="cb52-11"><a href="otherSF.html#cb52-11"></a>data =<span class="st"> </span><span class="kw">as.data.frame</span>(data1)</span></code></pre></div>
</div>
<div id="r语言调用matlab" class="section level2">
<h2><span class="header-section-number">9.2</span> R语言调用Matlab</h2>
<div id="强大的r.matlab包" class="section level3">
<h3><span class="header-section-number">9.2.1</span> 强大的<code>R.matlab</code>包</h3>
<p>一般工作流如下：</p>
<div class="sourceCode" id="cb53"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb53-1"><a href="otherSF.html#cb53-1"></a><span class="kw">library</span>(R.matlab)</span>
<span id="cb53-2"><a href="otherSF.html#cb53-2"></a>Matlab<span class="op">$</span><span class="kw">startServer</span>() <span class="co"># 启动matlab服务器，可能会较慢</span></span>
<span id="cb53-3"><a href="otherSF.html#cb53-3"></a><span class="co"># 创造与matlab交互的客户端对象，并看它的运行状态</span></span>
<span id="cb53-4"><a href="otherSF.html#cb53-4"></a>matlab &lt;-<span class="st"> </span><span class="kw">Matlab</span>()</span>
<span id="cb53-5"><a href="otherSF.html#cb53-5"></a><span class="kw">print</span>(matlab)</span>
<span id="cb53-6"><a href="otherSF.html#cb53-6"></a><span class="co"># 打开运行</span></span>
<span id="cb53-7"><a href="otherSF.html#cb53-7"></a>isOpen &lt;-<span class="st"> </span><span class="kw">open</span>(matlab)</span>
<span id="cb53-8"><a href="otherSF.html#cb53-8"></a><span class="kw">print</span>(matlab)</span>
<span id="cb53-9"><a href="otherSF.html#cb53-9"></a><span class="co"># matlab中运行脚本</span></span>
<span id="cb53-10"><a href="otherSF.html#cb53-10"></a><span class="kw">evaluate</span>(matlab, <span class="st">&quot;A = 1+2;&quot;</span>, <span class="st">&quot;B = ones(2, 20);&quot;</span>)</span>
<span id="cb53-11"><a href="otherSF.html#cb53-11"></a><span class="co"># 打印A的值</span></span>
<span id="cb53-12"><a href="otherSF.html#cb53-12"></a><span class="kw">evaluate</span>(matlab, <span class="st">&quot;A&quot;</span>)</span>
<span id="cb53-13"><a href="otherSF.html#cb53-13"></a><span class="co"># 将matlab中的值传到R中</span></span>
<span id="cb53-14"><a href="otherSF.html#cb53-14"></a>data &lt;-<span class="st"> </span><span class="kw">getVariable</span>(matlab, <span class="kw">c</span>(<span class="st">&quot;A&quot;</span>, <span class="st">&quot;B&quot;</span>))</span>
<span id="cb53-15"><a href="otherSF.html#cb53-15"></a><span class="co"># 将R中的值传到matlab中</span></span>
<span id="cb53-16"><a href="otherSF.html#cb53-16"></a>ABCD &lt;-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">rnorm</span>(<span class="dv">10000</span>), <span class="dt">ncol =</span> <span class="dv">100</span>)</span>
<span id="cb53-17"><a href="otherSF.html#cb53-17"></a><span class="kw">setVariable</span>(matlab, <span class="dt">ABCD =</span> ABCD)</span>
<span id="cb53-18"><a href="otherSF.html#cb53-18"></a><span class="co"># 关闭与matlab间的连接</span></span>
<span id="cb53-19"><a href="otherSF.html#cb53-19"></a><span class="kw">close</span>(matlab)</span></code></pre></div>
</div>
<div id="其他的一些小办法" class="section level3">
<h3><span class="header-section-number">9.2.2</span> 其他的一些小办法</h3>
<p>Matlab里面的三维画图比R要省事很多。这里探讨一下如何从R调用Matlab的一般步骤。</p>
<pre><code>- 安装R.matlab包。使用`writemat(filename,A=A,B=B)`把R里面的数据写进Matlab并保存成`.mat`格式。
- 安装matlabr包。使用`run_matlab_script`命令来执行一个`.m`脚本。或者使用`R.matlab`包里面的`evaluate`来一个一个地执行matlab命令。或者类似于调用stata：</code></pre>
<div class="sourceCode" id="cb55"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb55-1"><a href="otherSF.html#cb55-1"></a>    <span class="kw">library</span>(matlabr)</span>
<span id="cb55-2"><a href="otherSF.html#cb55-2"></a>    MatlabCode &lt;-<span class="st"> &#39;</span></span>
<span id="cb55-3"><a href="otherSF.html#cb55-3"></a><span class="st">    a = 3;</span></span>
<span id="cb55-4"><a href="otherSF.html#cb55-4"></a><span class="st">    b = a+1;</span></span>
<span id="cb55-5"><a href="otherSF.html#cb55-5"></a><span class="st">    &#39;</span></span>
<span id="cb55-6"><a href="otherSF.html#cb55-6"></a>    <span class="kw">run_matlab_code</span>(MatlabCode)</span></code></pre></div>
</div>
</div>
<div id="r与python的无缝对接" class="section level2">
<h2><span class="header-section-number">9.3</span> R与Python的无缝对接</h2>
<ul>
<li>第一步，首先配置好环境</li>
</ul>
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb56-1"><a href="otherSF.html#cb56-1"></a><span class="kw">library</span>(reticulate)</span>
<span id="cb56-2"><a href="otherSF.html#cb56-2"></a><span class="kw">use_condaenv</span>(<span class="st">&quot;D:/Anaconda3&quot;</span>)</span>
<span id="cb56-3"><a href="otherSF.html#cb56-3"></a></span>
<span id="cb56-4"><a href="otherSF.html#cb56-4"></a><span class="co"># 安装的python版本环境查看，显示anaconda和numpy的详细信息。放在</span></span>
<span id="cb56-5"><a href="otherSF.html#cb56-5"></a><span class="co"># use_condaenv()后，以使配置生效</span></span>
<span id="cb56-6"><a href="otherSF.html#cb56-6"></a><span class="kw">py_config</span>()</span>
<span id="cb56-7"><a href="otherSF.html#cb56-7"></a></span>
<span id="cb56-8"><a href="otherSF.html#cb56-8"></a><span class="kw">py_available</span>()<span class="co">#[1] TRUE   #检查您的系统是否安装过Python</span></span>
<span id="cb56-9"><a href="otherSF.html#cb56-9"></a><span class="kw">py_module_available</span>(<span class="st">&quot;pandas&quot;</span>)<span class="co">#检查“pandas”是否安装</span></span></code></pre></div>
<ul>
<li>第二步，调用有多种方法。我最喜欢这种，就是直接导入python模块，然后用R的风格来调用。此时R里面的美元符号$相当于python里面的“.”符号 ，如，</li>
</ul>
<div class="sourceCode" id="cb57"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb57-1"><a href="otherSF.html#cb57-1"></a>os &lt;-<span class="st"> </span><span class="kw">import</span>(<span class="st">&quot;os&quot;</span>)</span>
<span id="cb57-2"><a href="otherSF.html#cb57-2"></a>os<span class="op">$</span><span class="kw">getcwd</span>()</span>
<span id="cb57-3"><a href="otherSF.html#cb57-3"></a>os<span class="op">$</span><span class="kw">listdir</span>()<span class="co">#您可以使用os包中的listdir（）函数来查看工作目录中的所有文件</span></span>
<span id="cb57-4"><a href="otherSF.html#cb57-4"></a> </span>
<span id="cb57-5"><a href="otherSF.html#cb57-5"></a>numpy &lt;-<span class="st"> </span><span class="kw">import</span>(<span class="st">&quot;numpy&quot;</span>)</span>
<span id="cb57-6"><a href="otherSF.html#cb57-6"></a>y &lt;-<span class="st"> </span><span class="kw">array</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">4</span>, <span class="kw">c</span>(<span class="dv">2</span>, <span class="dv">2</span>))</span>
<span id="cb57-7"><a href="otherSF.html#cb57-7"></a>y</span>
<span id="cb57-8"><a href="otherSF.html#cb57-8"></a>x &lt;-<span class="st"> </span>numpy<span class="op">$</span><span class="kw">array</span>(y)</span>
<span id="cb57-9"><a href="otherSF.html#cb57-9"></a>x</span>
<span id="cb57-10"><a href="otherSF.html#cb57-10"></a>numpy<span class="op">$</span><span class="kw">transpose</span>(x)<span class="co">#将数组进行转置</span></span>
<span id="cb57-11"><a href="otherSF.html#cb57-11"></a>numpy<span class="op">$</span>linalg<span class="op">$</span><span class="kw">eig</span>(x)<span class="co">#求特征根和特征向量</span></span></code></pre></div>
<div id="其他" class="section level3">
<h3><span class="header-section-number">9.3.1</span> 其他：</h3>
<ul>
<li>当你发现有些包没有，需要安装的时候，可以如下，</li>
</ul>
<div class="sourceCode" id="cb58"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb58-1"><a href="otherSF.html#cb58-1"></a><span class="kw">library</span>(reticulate)</span>
<span id="cb58-2"><a href="otherSF.html#cb58-2"></a></span>
<span id="cb58-3"><a href="otherSF.html#cb58-3"></a><span class="co"># create a new environment </span></span>
<span id="cb58-4"><a href="otherSF.html#cb58-4"></a><span class="kw">conda_create</span>(<span class="st">&quot;r-reticulate&quot;</span>)</span>
<span id="cb58-5"><a href="otherSF.html#cb58-5"></a></span>
<span id="cb58-6"><a href="otherSF.html#cb58-6"></a><span class="co"># install SciPy</span></span>
<span id="cb58-7"><a href="otherSF.html#cb58-7"></a><span class="kw">conda_install</span>(<span class="st">&quot;r-reticulate&quot;</span>, <span class="st">&quot;scipy&quot;</span>)</span>
<span id="cb58-8"><a href="otherSF.html#cb58-8"></a></span>
<span id="cb58-9"><a href="otherSF.html#cb58-9"></a><span class="co"># import SciPy (it will be automatically discovered in &quot;r-reticulate&quot;)</span></span>
<span id="cb58-10"><a href="otherSF.html#cb58-10"></a>scipy &lt;-<span class="st"> </span><span class="kw">import</span>(<span class="st">&quot;scipy&quot;</span>)</span></code></pre></div>
<ul>
<li>这是调用时通常需要的代码：</li>
</ul>
<div class="sourceCode" id="cb59"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb59-1"><a href="otherSF.html#cb59-1"></a><span class="kw">library</span>(reticulate)</span>
<span id="cb59-2"><a href="otherSF.html#cb59-2"></a><span class="co"># 可以查你有几个版本的python</span></span>
<span id="cb59-3"><a href="otherSF.html#cb59-3"></a><span class="kw">py_config</span>()</span>
<span id="cb59-4"><a href="otherSF.html#cb59-4"></a><span class="co"># 想使用哪个版本的python</span></span>
<span id="cb59-5"><a href="otherSF.html#cb59-5"></a><span class="kw">use_python</span>(<span class="st">&#39;C:/Users/sheng/AppData/Local/Continuum/anaconda3/python.exe&#39;</span>)</span>
<span id="cb59-6"><a href="otherSF.html#cb59-6"></a><span class="co"># 检查python可不可用</span></span>
<span id="cb59-7"><a href="otherSF.html#cb59-7"></a><span class="kw">py_available</span>()</span>
<span id="cb59-8"><a href="otherSF.html#cb59-8"></a><span class="co"># 检查模块可不可用</span></span>
<span id="cb59-9"><a href="otherSF.html#cb59-9"></a><span class="kw">py_module_available</span>(<span class="st">&#39;tushare&#39;</span>)</span></code></pre></div>
</div>
</div>
<div id="与julia的对接" class="section level2">
<h2><span class="header-section-number">9.4</span> 与julia的对接</h2>
<div id="juliacall包" class="section level3">
<h3><span class="header-section-number">9.4.1</span> <code>JuliaCall</code>包</h3>
<p>感觉此包没有类似<code>reticulate</code>包调用python那么无缝。</p>
<ul>
<li>在R中执行julia脚本</li>
</ul>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb60-1"><a href="otherSF.html#cb60-1"></a><span class="kw">library</span>(JuliaCall)</span>
<span id="cb60-2"><a href="otherSF.html#cb60-2"></a><span class="co"># 设置存放julia二进制文件的文件夹</span></span>
<span id="cb60-3"><a href="otherSF.html#cb60-3"></a><span class="kw">julia_setup</span>(<span class="dt">JULIA_HOME =</span> <span class="st">&#39;D:/Program Files/Julia-1.4.2/bin&#39;</span>)</span>
<span id="cb60-4"><a href="otherSF.html#cb60-4"></a><span class="co"># 几种主要的调用方式，我把我喜欢的写出来的</span></span>
<span id="cb60-5"><a href="otherSF.html#cb60-5"></a>julia<span class="op">$</span><span class="kw">command</span>(<span class="st">&quot;a = sqrt(2);&quot;</span>) <span class="co"># 在julia环境中产生了变量a</span></span>
<span id="cb60-6"><a href="otherSF.html#cb60-6"></a>ans &lt;-<span class="st"> </span>julia<span class="op">$</span><span class="kw">eval</span>(<span class="st">&quot;a&quot;</span>) <span class="co"># 把变量a的值传给R环境中的ans变量</span></span>
<span id="cb60-7"><a href="otherSF.html#cb60-7"></a><span class="co"># 其他的调用方式</span></span>
<span id="cb60-8"><a href="otherSF.html#cb60-8"></a><span class="kw">julia_command</span>(<span class="st">&quot;a = sqrt(2);&quot;</span>)</span>
<span id="cb60-9"><a href="otherSF.html#cb60-9"></a><span class="kw">julia_eval</span>(<span class="st">&quot;a&quot;</span>)</span>
<span id="cb60-10"><a href="otherSF.html#cb60-10"></a><span class="co">#&gt; [1] 1.414214</span></span>
<span id="cb60-11"><a href="otherSF.html#cb60-11"></a><span class="dv">2</span> <span class="op">%&gt;J%</span><span class="st"> </span>sqrt</span>
<span id="cb60-12"><a href="otherSF.html#cb60-12"></a><span class="co">#&gt; [1] 1.414214</span></span></code></pre></div>
<ul>
<li>R与julia互传变量：前面提到的<code>julia_eval</code>可以把julia中的变量传出来，使用<code>julia_assign</code>可以把R中的变量传到julia中去。</li>
</ul>
<div class="sourceCode" id="cb61"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb61-1"><a href="otherSF.html#cb61-1"></a><span class="kw">julia_assign</span>(<span class="st">&#39;a&#39;</span>,<span class="dv">1</span><span class="op">:</span><span class="dv">5</span>)</span>
<span id="cb61-2"><a href="otherSF.html#cb61-2"></a><span class="kw">julia_command</span>(<span class="st">&#39;a&#39;</span>)</span></code></pre></div>
<ul>
<li>julia控制台，而且只要你前期<code>julia_setup()</code>了，这个控制台里面就包含了前期运算时的变量</li>
</ul>
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb62-1"><a href="otherSF.html#cb62-1"></a><span class="kw">julia_console</span>()</span>
<span id="cb62-2"><a href="otherSF.html#cb62-2"></a><span class="co"># 输入exit 可以退出</span></span>
<span id="cb62-3"><a href="otherSF.html#cb62-3"></a><span class="co"># julia&gt; exit</span></span></code></pre></div>
<ul>
<li>它的函数调用非常吸引人：你甚至可以用R对象作为julia函数的参数</li>
</ul>
<div class="sourceCode" id="cb63"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb63-1"><a href="otherSF.html#cb63-1"></a><span class="kw">julia_install_package_if_needed</span>(<span class="st">&quot;Optim&quot;</span>)</span>
<span id="cb63-2"><a href="otherSF.html#cb63-2"></a>  opt &lt;-<span class="st"> </span><span class="kw">julia_pkg_import</span>(<span class="st">&quot;Optim&quot;</span>,</span>
<span id="cb63-3"><a href="otherSF.html#cb63-3"></a>                           <span class="dt">func_list =</span> <span class="kw">c</span>(<span class="st">&quot;optimize&quot;</span>, <span class="st">&quot;BFGS&quot;</span>))</span>
<span id="cb63-4"><a href="otherSF.html#cb63-4"></a>rosenbrock &lt;-<span class="st"> </span><span class="cf">function</span>(x) (<span class="fl">1.0</span> <span class="op">-</span><span class="st"> </span>x[<span class="dv">1</span>])<span class="op">^</span><span class="dv">2</span> <span class="op">+</span><span class="st"> </span><span class="fl">100.0</span> <span class="op">*</span><span class="st"> </span>(x[<span class="dv">2</span>] <span class="op">-</span><span class="st"> </span>x[<span class="dv">1</span>]<span class="op">^</span><span class="dv">2</span>)<span class="op">^</span><span class="dv">2</span></span>
<span id="cb63-5"><a href="otherSF.html#cb63-5"></a>result &lt;-<span class="st"> </span>opt<span class="op">$</span><span class="kw">optimize</span>(rosenbrock, <span class="kw">rep</span>(<span class="dv">0</span>,<span class="dv">2</span>), opt<span class="op">$</span><span class="kw">BFGS</span>())</span>
<span id="cb63-6"><a href="otherSF.html#cb63-6"></a>result</span></code></pre></div>

</div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="ML.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="DrawTips.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["CBook.pdf"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
